<template>
  <el-dialog :title="!dataForm.id ? '门店支付参数配置新增' : '门店支付参数配置修改'" :close-on-click-modal="false" :visible.sync="visible"  center append-to-body>
    <el-button class="miui-dialog-close-btn" @click="visible = false">关闭页面</el-button>
    <el-form class="verify-form" :model="dataForm" :rules="dataRule" ref="dataForm" label-width="100px">
    <el-form-item label="销售部门" prop="deptId">
      <el-input v-model="dataForm.deptName" placeholder="销售部门" readonly>
        <el-button slot="append" :disabled="!!dataForm.id" @click="deptClickHandle">选择</el-button>
      </el-input>
    </el-form-item>
    <el-form-item label="微信公众号" prop="wechatId">
      <el-select v-model="dataForm.wechatId">
        <el-option v-for="item in wxAccounts" :key="item.id" :value="item.id" :label="item.name"></el-option>
      </el-select>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
    <search-dept v-if="deptVisible" ref="deptDialog" @refreshData="getDeptData"></search-dept>
  </el-dialog>
</template>

<script>
    import {getWxDeptLinkObj, addWxDeptLinkObj, putWxDeptLinkObj} from '@/api/mp/wxdeptlink'
    import {fetchAccountList} from '@/api/mp/wxaccount'
    import SearchDept from './SearchDept'

    export default {
    data () {
      return {
        visible: false,
        deptVisible: false,
        dataForm: {
          id: 0,
          deptId: '',
          wechatId: ''
        },
        wxAccounts: [],
        dataRule: {
          deptId: [
            { required: true, message: '部门id不能为空', trigger: 'blur' }
          ],
        }
      }
    },
    created() {
      this.getWxAccountList()
    },
    components: {
      SearchDept
    },
    methods: {
      init (id) {
        if(this.dataForm.id == 0){
          for(let key in this.dataForm){
            this.dataForm[key] = ''
          }
        }
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            getWxDeptLinkObj(this.dataForm.id).then(response => {
                this.dataForm = response.data.data
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            if (this.dataForm.id) {
                putWxDeptLinkObj(this.dataForm).then(data => {
                    this.$message.success('修改成功')
                    this.visible = false
                    this.$emit('refreshDataList')
                });
            } else {
                addWxDeptLinkObj(this.dataForm).then(data => {
                    this.$message.success('添加成功')
                    this.visible = false
                    this.$emit('refreshDataList')
                })
            }
          }
        })
      },
      getWxAccountList(){
        fetchAccountList().then(result => {
          this.wxAccounts = result.data.data
        })
      },
      // 选择部门
      deptClickHandle(){
        this.deptVisible = true
        this.$nextTick(() => {
          this.$refs.deptDialog.init(this.dataForm.deptId)
        })
      },
      // 获取选择部门信息
      getDeptData(data){
        this.$set(this.dataForm,'deptId',data.id)
        this.$set(this.dataForm,'deptName',data.name)
        this.dataForm = {...this.dataForm}
      },
    }
  }
</script>
